package com.windoent.yugioh.ui.activity.meinfo;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mobstat.StatService;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.windoent.yugioh.R;
import com.windoent.yugioh.adapter.brvah.ChangeRoleAdapter;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.ChangeRoleBean;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.dialog.SucessFailAutoDismissDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/8 17:15
 * desc   :切换身份
 */
public class ChangeIdentyActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private RecyclerView mRecyclerView;
    private int current_role;
    private boolean isCustomer;
    private Intent intent;
    private boolean isReferee;

    @Override
    public int getLayout() {
        return R.layout.activity_change_identy;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    }

    @Override
    public void initData() {
        super.initData();
        intent = getIntent();
        current_role = intent.getIntExtra("current_role", 0);
        isCustomer = intent.getBooleanExtra("is_customer", false);
        isReferee = intent.getBooleanExtra("is_referee", false);
        initAdapter();
    }

    private void initAdapter() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        ChangeRoleAdapter adapter = new ChangeRoleAdapter();
        adapter.setNewInstance(getRolesList());
        adapter.setDefaultCheck(current_role);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> baseQuickAdapter, @NonNull View view, int position) {
                List<ChangeRoleBean> data = adapter.getData();
                if (data != null) {
                    ChangeRoleBean bean = data.get(position);
                    String name = bean.getName();
                    changeRole(getUserRoleIdentifyType(name));
                }
            }
        });
    }

    //角色字符串
    private String getUserRoleIdentifyType(String s) {
        if (BaseMethod.notNull(s) && s.equals(getString(R.string.string_role_caipan))) {
            return "referee";
        }
        if (BaseMethod.notNull(s) && s.equals(getString(R.string.string_role_dianjia))) {
            return "customer";
        }
        return "players";
    }

    //角色位置0，选手，1裁判，2店家
    private int getRolePosition(String s) {
        if (BaseMethod.notNull(s) && s.equals("referee")) {
            return 1;
        }
        if (BaseMethod.notNull(s) && s.equals("customer")) {
            return 2;
        }
        return 0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    //切换角色
    private void changeRole(String role) {
        showLoadingDialog("");
        PhoneLive.changeRole(getUserToken(), role, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean json = gson.fromJson(response, GeneralBean.class);
                            current_role = getRolePosition(role);
                            Intent intent = new Intent();
                            intent.putExtra("role", current_role);
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }

            }
        });
    }

    private List<ChangeRoleBean> getRolesList() {
        List<ChangeRoleBean> list = new ArrayList<>();
        list.clear();
        list.add(new ChangeRoleBean(R.drawable.drawable_role_xuanshou, getString(R.string.string_role_xuanshou)));
        if (isReferee)
            list.add(new ChangeRoleBean(R.drawable.drawable_role_caipan, getString(R.string.string_role_caipan)));
        if (isCustomer)
            list.add(new ChangeRoleBean(R.drawable.drawable_role_dianjia, getString(R.string.string_role_dianjia)));
        return list;
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "切换身份界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "切换身份界面");
    }
}
